package com.amgl.mobike.base;

import android.content.Context;
import android.support.v4.app.Fragment;

import com.amgl.mobike.app.IImageLoader;
import com.amgl.mobike.app.glide.FragmentImageLoader;

import timber.log.Timber;

/**
 * Created by 阿木 on 2017/4/7.
 */

public class BaseFragment extends Fragment {
    private boolean mIsFirstVisible = true;
    private static final int CODE_RESUME = 0x00;
    private static final int CODE_USER_VISIBLE_HINT = 0x01;
    private static final int CODE_PAUSE = 0x02;
    private static final int CODE_HIDDEN_CHANGED = 0x03;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Timber.v("on attach");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mIsFirstVisible = true;
        Timber.v("on detach");
    }

    @Override
    public void onResume() {
        super.onResume();
        Timber.v("(%s) on resume, userVisibleHint: %s", getPageID(), getUserVisibleHint());
        if (getUserVisibleHint()) {
            if (mIsFirstVisible) {
                mIsFirstVisible = false;
                onFragmentFirstShow(CODE_RESUME);
            } else {
                onFragmentShow(CODE_RESUME);
            }
        } else {
            Timber.v("(%s) getUserVisibleHint false, onResume", getPageID());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Timber.v("(%s) on pause, userVisibleHint: %s", getPageID(), getUserVisibleHint());
        if (getUserVisibleHint()) {
            onFragmentHide(CODE_PAUSE);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        Timber.v("(%s) on stop", getPageID());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Timber.v("(%s) on hidden changed, hidden: %s", getPageID(), hidden);
        if (hidden)
            onFragmentHide(CODE_HIDDEN_CHANGED);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Timber.v("(%s) setUserVisibleHint: %s", getPageID(), isVisibleToUser);
        if (isResumed()) {
            if (isVisibleToUser) {
                if (mIsFirstVisible) {
                    mIsFirstVisible = false;
                    onFragmentFirstShow(CODE_USER_VISIBLE_HINT);
                } else {
                    onFragmentShow(CODE_USER_VISIBLE_HINT);
                }
            } else {
                onFragmentHide(CODE_USER_VISIBLE_HINT);
            }
        } else {
            Timber.v("(%s) is not resumed, setUserVisibleHint: %s", getPageID(), isVisibleToUser);
        }
    }

    public void forceLoad() {
        Timber.v("(%s) forceLoad", getPageID());
    }

    protected void onFragmentFirstShow(int code) {
        Timber.v("(%s) onFragmentFirstShow(%s)", getPageID(), code);
    }

    protected void onFragmentShow(int code) {
        Timber.v("(%s) onFragmentShow(%s)", getPageID(), code);
    }

    protected void onFragmentHide(int code) {
        Timber.v("(%s) onFragmentHide(%s)", getPageID(), code);
    }

    protected String getPageID() {
        return String.format("%s, %d", this.getClass().getSimpleName(), this.getId());
    }

    public String getPageTitle() {
        return this.getClass().getSimpleName();
    }

    public static boolean isFragmentVisible(Fragment fragment) {
        return !fragment.isHidden() && fragment.getUserVisibleHint();
    }

    public static IImageLoader createImageLoader(Fragment fragment) {
        FragmentImageLoader imageLoader = new FragmentImageLoader(fragment);
        return imageLoader;
    }
}
